package bean;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import utils.PhoneUtils;

public class UserRank {

    private User user;
    private int rank;
    
    public UserRank() {
        this.user = null;
        this.rank = -1;
    }
    
    public UserRank(User user, int rank) {
        this.user = user;
        this.rank = rank;
    }
    
    public static JSONArray toJSONArray(List<UserRank> list) {
        JSONArray array = new JSONArray();
        if (list != null) {
            for (UserRank u : list) {
                array.put(toJSONObj(u));
            }
        }
        return array;
    }

    /**
     * 把代理对象转换为JSONObject
     * @param account
     * @return
     */
    public static final JSONObject toJSONObj(UserRank userRank) {
        JSONObject obj = new JSONObject();
        User user = userRank.getUser();
        int rank = userRank.getRank();
        obj.put("userId", user.getUserId());
        obj.put("userName", user.getUserName());
        obj.put("avatar", user.getAvatar());
        obj.put("support", user.getSupport());
        obj.put("openId", user.getOpenId());
        obj.put("tag", user.getTag());
        obj.put("phone", PhoneUtils.hideCentralNum(user.getPhone()));
        obj.put("rank", rank);
        return obj;
    }
    
    public User getUser() {
        return user;
    }
    
    public void setUser(User user) {
        this.user = user;
    }
    
    public int getRank() {
        return rank;
    }
    
    public void setRank(int rank) {
        this.rank = rank;
    }
    
    public static void main(String[] args) {
        UserRank ur = new UserRank();
        User u = new User("1", "name", "18829092898", ".;;;", "1", 112, "未知");
        List<UserRank> l = new ArrayList<UserRank>();
        ur.setRank(1);
        ur.setUser(u);
        l.add(ur);
        
        UserRank ur2 = new UserRank();
        ur2.setRank(1);
        ur2.setUser(u);
        l.add(ur2);
        
        JSONArray a = toJSONArray(l);
        System.out.println(a.toString());
    }
    
}
